package com.kaihong.template.ble.operation;

import com.kaihong.template.ble.operation.callback.adapter.BluetoothAdapterStateChangeCallback;
import com.kaihong.template.ble.operation.callback.adapter.BluetoothDeviceFoundCallback;
import com.kaihong.template.ble.operation.callback.gatt.BleCharacteristicValueChangeCallback;
import com.kaihong.template.ble.operation.callback.gatt.BleConnectionStateChangeCallback;
import com.kaihong.template.ble.operation.callback.gatt.BleServicesDiscoveredCallback;
import com.kaihong.template.ble.operation.callback.gatt.EnableNotifyIndicateCallback;
import com.kaihong.template.ble.operation.callback.gatt.ReadBleCharacteristicValueCallback;
import com.kaihong.template.ble.operation.callback.gatt.ReadBleDescriptorValueCallback;
import com.kaihong.template.ble.operation.callback.gatt.WriteBleCharacteristicValueCallback;
import com.kaihong.template.ble.operation.callback.gatt.WriteBleDescriptorValueCallback;
import com.kaihong.template.ble.operation.model.DescriptorData;

import java.util.UUID;


/**
 * Ble Operator
 */
public interface BleOperator {
    /**
     * Turn on the phone's bluetooth
     *
     * @return String The return value of the Bluetooth connection status
     */
    String openBluetoothAdapter();

    /**
     * Turn off mobile phone Bluetooth
     *
     * @return String Turn off the return value of Bluetooth
     */
    String closeBluetoothAdapter();

    /**
     * Get Ble Bluetooth connection status
     */
    boolean getBluetoothAdapterState();

    /**
     * Monitor Bluetooth state changes
     *
     * @param stateChangeCallback Bluetooth adapter status change callback
     */
    void onBluetoothAdapterStateChange(BluetoothAdapterStateChangeCallback stateChangeCallback);

    /**
     * Get device id
     *
     * @return The id of the connected device
     */
    String getDeviceId();

    /**
     * Start searching for nearby Bluetooth peripherals
     *
     * @param services             services
     * @param isAllowDuplicatesKey Whether to allow repeated reporting of the same device true means not allowed,
     *                             false means allowed
     * @param interval             The interval for reporting the device, the default is 0
     * @return Result of string
     */
    String startBluetoothDevicesDiscovery(UUID[] services, String mac, boolean isAllowDuplicatesKey, int interval);

    /**
     * Stop searching for nearby Bluetooth peripherals
     *
     * @return Result of string
     */
    String stopBluetoothDevicesDiscovery();

    /**
     * Subscribe to the event of discovering a new Bluetooth device
     *
     * @param deviceFoundCallback Callback for discovering a new Bluetooth device
     */
    void onBluetoothDeviceFound(BluetoothDeviceFoundCallback deviceFoundCallback);

    /**
     * Connect Ble Bluetooth device
     *
     * @param deviceId The id of the connected device
     * @return String
     */
    String createBleConnection(String deviceId);

    /**
     * Turn off the Ble Bluetooth device
     *
     * @param deviceId The id of the closed device
     * @return String
     */
    String closeBleConnection(String deviceId);

    /**
     * Register a callback function to get the connection status change
     *
     * @param connectionStateChangeCallback Ble Callback for device connection status changes
     */
    void onBleConnectionStateChange(BleConnectionStateChangeCallback connectionStateChangeCallback);

    /**
     * Register a callback function to get service discovery
     *
     * @param servicesDiscoveredCallback Discovery service callback
     */
    void onBleServicesDiscovered(BleServicesDiscoveredCallback servicesDiscoveredCallback);

    /**
     * Read the specified value from the specified service id and specified Characteristic
     *
     * @param deviceId                           device Id
     * @param serviceId                          service Id
     * @param characteristicId                   characteristic Id
     * @param readBleCharacteristicValueCallback Read feature worth callback function
     */
    void readBleCharacteristicValue(String deviceId, String serviceId, String characteristicId,
                                    ReadBleCharacteristicValueCallback readBleCharacteristicValueCallback);

    /**
     * Call to get, used to write the specified value to the specified service id and specified Characteristic
     *
     * @param descriptorData              descriptorData
     * @param writeCharacteristicCallback Callback of the result of writing the characteristic value
     */
    void writeBleCharacteristicValue(DescriptorData descriptorData,
                                     WriteBleCharacteristicValueCallback writeCharacteristicCallback);

    /**
     * Set whether to enable the characteristic notify or indicate of the specified service, and the mode is determined
     * by the isIndicate set by the user
     *
     * @param deviceId         device Id
     * @param serviceId        service Id
     * @param characteristicId characteristic Id
     * @param canNotify        true is to start the notification, false is to close the notification
     * @return String The result of the function setting enable report except for ERRCODE_OK, others are all failures
     */
    String notifyBleCharacteristicValueChange(String deviceId, String serviceId, String characteristicId,
                                              boolean canNotify);

    /**
     * Register a callback function to get the write descriptor enable notification/indication result
     *
     * @param enableNotifyIndicateCallback the result of callback
     */
    void onEnableNotifyIndicate(EnableNotifyIndicateCallback enableNotifyIndicateCallback);

    /**
     * Read the specified value from the specified service id and specified Descriptor
     *
     * @param deviceId                       device Id
     * @param serviceId                      service Id
     * @param characteristicId               Characteristic id
     * @param descriptorId                   Descriptor id
     * @param readBleDescriptorValueCallback Read feature worth callback function
     *                                       Parameter 1: data Parameter 2: errCode These two parameters need to be
     * @return string returned by the json result
     */
    String readBleDescriptorValue(String deviceId, String serviceId, String characteristicId, String descriptorId,
                                  ReadBleDescriptorValueCallback readBleDescriptorValueCallback);

    /**
     * Used to write the specified value to the specified service id and specified Descriptor
     *
     * @param descriptorData          descriptorData
     * @param writeDescriptorCallback Callback of the result of writing the characteristic value
     * @return the result of string
     */
    String writeBleDescriptorValue(DescriptorData descriptorData,
                                   WriteBleDescriptorValueCallback writeDescriptorCallback);

    /**
     * Set up an interface that can be in Bluetooth mode
     *
     * @param isEnableIndication true:Enable instructions false:Disable instructions, enable notifications
     */
    void setEnableIndication(boolean isEnableIndication);

    /**
     * Register a callback function to get Characteristic changes
     *
     * @param characteristicValueChangeCallback Feature change callback
     *                                          param1：deviceId param2：characteristicId param3：data
     */
    void onBleCharacteristicValueChange(BleCharacteristicValueChangeCallback characteristicValueChangeCallback);

    /**
     * Get Ble connection status
     *
     * @param deviceId                      ble device id
     * @return resultCode of string
     */
    boolean getBleConnectionState(String deviceId);
}
